package cn.chencq.proxy.timer.handler.custom;

import java.net.Proxy;
import java.util.HashSet;
import java.util.Set;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import cn.chencq.proxy.timer.handler.GeneralProxyHandler;
/**
 * @website http://www.kxdaili.com
 * @author chencq
 * @desc kxdl 10分钟更新
 * 
 */
public class ProxyHandlerByKXDL extends GeneralProxyHandler {

	@Override
	public String getHandler() {

		return "KXDL";
	}

	@Override
	public int parseResp(String rid,Document doc) {
		Set<String> proxySet = new HashSet<String>();
		Elements table = doc.select("table");
		for (Element tb : table) {
			Elements trs = tb.getElementsByTag("tr");
			for (int i = 1; i < trs.size(); i++) {
				Elements tds = trs.get(i).getElementsByTag("td");
				String ip = tds.get(0).html();
				String port = tds.get(1).html();
				proxySet.add(ip + ":" + port);
			}
		}
		super.putToCache(rid,proxySet);
		return proxySet.size();
	}
	
	@Override
	public int crawler(String rid,String url,Proxy proxy) {
		int ret = 0;
		try {
			Document doc = requestDoc(rid,url,proxy);
			if (doc != null) {
				ret = parseResp(rid,doc);
			} else {
				ret = 0;
			}
		} catch (Exception e) {
			ret = 0;
		}
		return ret;
	}

}
